package cn.msxb.clustersession.controller;

import cn.msxb.clustersession.bean.ReturnMsg;
import cn.msxb.clustersession.bean.User;
import cn.msxb.clustersession.pojo.Resume;
import cn.msxb.clustersession.service.ResumeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 测试建立控制层
 */
@Controller
@RequestMapping("/test")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    // 127.0.0.1:8080/test/queryAllResume
    @RequestMapping("/queryAllResume")
    public String queryAllResume(Model model) {
        model.addAttribute("list", resumeService.queryAllResume());
        return "success";
    }

    @RequestMapping("/delteResume")
    @ResponseBody
    public String delteResume(@RequestBody Resume resume) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            resumeService.deleteById(Long.valueOf(resume.getId()));
            return objectMapper.writeValueAsString(new ReturnMsg("0000", "删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return objectMapper.writeValueAsString(new ReturnMsg("9999", "删除失败"));
        }
    }

    // 127.0.0.1:8080/test/login
    @RequestMapping("/insertResume")
    public String insertResume() {
        return "insertResume";
    }

    @RequestMapping("/saveResume")
    @ResponseBody
    public String saveResume(@RequestBody Resume resume) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Resume save = resumeService.save(resume);
            if (save.getId() != null) {
                return objectMapper.writeValueAsString(new ReturnMsg("0000", "新增成功"));
            } else {
                throw new RuntimeException("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return objectMapper.writeValueAsString(new ReturnMsg("9999", "新增失败"));
        }
    }


    // 127.0.0.1:8080/test/login
    @RequestMapping("/login")
    public String login(HttpSession session) {
        session.setAttribute("sessionId",session.getId());
        return "login";
    }

    // 127.0.0.1:8080/test/loginVerify
    @RequestMapping("/loginVerify")
    @ResponseBody
    public String loginVerify(HttpSession session, @RequestBody User user) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        if ("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())) {
            session.setAttribute("sessionId",session.getId());
            return objectMapper.writeValueAsString(new ReturnMsg("0000", "登陆成功"));
        }
        return objectMapper.writeValueAsString(new ReturnMsg("9999", "密码错误"));
    }
}
